package com.luoye.unionlotto;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class UnionlottoFromFileAndNetAnalyzer extends UnionlottoAnalyzer{
    private UnionlottoFromFileGetter unionlottoFromFileGetter;
    private UnionlottoFromNetGetter unionlottoFromNetGetter;
    private UnionlottoToFileWriter unionlottoToFileWriter;
    private BuysUnionlottoFileReaderWriter buysUnionlottoFileReaderWriter;

    public UnionlottoFromFileAndNetAnalyzer(String dataDir,String url) {
        this.unionlottoFromFileGetter=new UnionlottoFromFileGetter(dataDir);
        this.unionlottoFromNetGetter=new UnionlottoFromNetGetter(url);
        this.unionlottoToFileWriter=new UnionlottoToFileWriter(dataDir);
        this.buysUnionlottoFileReaderWriter=new BuysUnionlottoFileReaderWriter(dataDir);
    }
    public void analyze(){
        //先从文件读取往期号码
        this.unionlottoList=this.unionlottoFromFileGetter.get();
        Unionlotto latest=null;
        if(this.unionlottoList.size()>0){
            latest=this.unionlottoList.get(0);
        }
        //从网络获取
        List<Unionlotto> unionlottoListTemp=this.unionlottoFromNetGetter.get(latest);
       if(latest==null){
           this.unionlottoList=unionlottoListTemp;
       }else {
           List<Unionlotto> unionlottoListBase = new ArrayList<Unionlotto>();
           for (Unionlotto unionlotto : unionlottoListTemp) {
               if (unionlotto.getDate().equals(latest.getDate())) {
                   break;
               }
               unionlottoListBase.add(unionlotto);
           }
           unionlottoListBase.addAll(this.unionlottoList);
           this.unionlottoList = unionlottoListBase;
       }
        //写文件
        this.unionlottoToFileWriter.write(this.unionlottoList);

        System.out.println(this.unionlottoList.size());

        //预测方法1
        //List<Unionlotto> result1=this.fullMaxAndMin();
        //预测方法2
        List<Unionlotto> result1=this.randMaxAndMin();
        List<Unionlotto> result2=new ArrayList<Unionlotto>();
        result2.add(new Unionlotto("追号一组", Arrays.asList(new String[]{"01","04","15","18","22","32"}),"14"));
        result2.add(new Unionlotto("追号二组", Arrays.asList(new String[]{"16","19","23","25","30","33"}),"08"));
        result2.add(new Unionlotto("追号三组", Arrays.asList(new String[]{"17","29","13","26","31","24"}),"09"));

        //预测方法3
        List<Unionlotto> result3=this.sumOneByOne();
        //预测方法4
        List<Unionlotto> result4=this.randOne();

        //数据结果
        System.out.println("");
        System.out.println("");
        printResult(result1);
        printResult(result2);
        printResult(result3);
        printResult(result4);
        //结果写入文件
        List<Unionlotto> results = new ArrayList<Unionlotto>();
        results.addAll(result1);
        results.addAll(result2);
        results.addAll(result3);
        results.addAll(result4);

        List<Unionlotto> unionlottos = this.fillVacancy(results);
        printResult(unionlottos);

        //计算日期
        Date now=new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        int d=0;
        if(w>0&&w<=2){//星期2的
            d=2-w;
        }else if(w>2&&w<=4){//星期4的
            d=4-w;
        }else{
            d=7-w;
        }
        cal.add(Calendar.DATE,d);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date=dateFormat.format(cal.getTime());
        if(w==0||w==2||w==4){
            cal.setTime(now);
            if(cal.get(Calendar.HOUR_OF_DAY)>=20){
                System.out.println("购买时间已过");
                return;
            }
        }
        this.buysUnionlottoFileReaderWriter.write(date,results);
    }

    public static void main(String args[]){
        UnionlottoFromFileAndNetAnalyzer unionlottoAnalyzer=new UnionlottoFromFileAndNetAnalyzer("D:\\\\download\\\\unionlotto","http://kaijiang.zhcw.com/zhcw/inc/ssq/ssq_wqhg.jsp");
        unionlottoAnalyzer.analyze();
    }
}
